﻿using BLL;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Model;
using StackExchange.Redis;
using System.Net;
using System.Security.Cryptography;
using System;

namespace TiboShop.Controllers
{
    public class OrderController : Controller
    {
        private readonly ISession _session;
        private readonly IHttpContextAccessor _contextAccessor;
        private readonly BLLOrder bllOrder;
        public OrderController(BLLOrder order,IHttpContextAccessor accessor) 
        {
            bllOrder = order;
            _contextAccessor = accessor;
            _session = _contextAccessor.HttpContext!.Session;
        }
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public bool AddToOrder(int cid,int num)
        {
            if(bllOrder.AddCommodityInOrder(Convert.ToInt32(_session.GetInt32("UID")), cid, num))
            {
               
                return true;
            }
         
            return false;
        }
        [HttpGet]
        public IActionResult GetUserCart()
        {
            List<Model.Order> orders = bllOrder.GetGivenOrder(Convert.ToInt32(_session.GetInt32("UID")), "已创建");
            return View(orders[0].ordercommodity);
        }
        [HttpGet]
        public IActionResult BuyNow(int cid, int num,string address) 
        {
            if(bllOrder.BuyNow(Convert.ToInt32(_session.GetInt32("UID")), cid, num,address))
            {
                MessageBoxVO messageBoxVO = new MessageBoxVO();
                messageBoxVO.Msg = "购买成功！请前往订单中查看";
                messageBoxVO.RedrectUrl = "/Commodity/Index?cid="+cid;
                return View("Views\\Shared\\Components\\MessageBox\\Default.cshtml", messageBoxVO);
            }
            else
            {
                MessageBoxVO messageBoxVO = new MessageBoxVO();
                messageBoxVO.Msg = "购买失败";
                messageBoxVO.RedrectUrl = "/Commodity/Index?cid=" + cid;
                return View("Views\\Shared\\Components\\MessageBox\\Default.cshtml", messageBoxVO);
            }
        }
        [HttpGet]
        public IActionResult GetallOrder()
        {
            ViewBag.role = Convert.ToInt32(_session.GetInt32("URID"));
            
			return View(bllOrder.GetUserAllOrder(Convert.ToInt32(_session.GetInt32("UID"))));
        }
		[HttpPost]
		public IActionResult BuyInCart(string result,string address)
		{

            string[] strs = result.Split('+');
           
            for(int i = 0;i<(strs.Count()-1);i++)
            {
                string[] comdity= strs[i].Split(","); 
                int cid = int.Parse(comdity[0]);
                int num = int.Parse(comdity[1]);
				bllOrder.BuyNowThen(Convert.ToInt32(_session.GetInt32("UID")), cid, num, address);
			}
            bllOrder.Recover(Convert.ToInt32(_session.GetInt32("UID")));
            return RedirectToAction("GetUserCart");
        }
        [HttpPost]
        public IActionResult OrderHandle(int oid,string state)
        {
            Console.WriteLine("OrderHandle");
            bllOrder.changeOrderState(oid, state);
            return RedirectToAction("GetallOrder");
        }
        [HttpPost]
        public IActionResult deleOrder(int oid)
        {
            bllOrder.DeleteOrder(oid);
            return RedirectToAction("GetallOrder");
        }
    }
}
